import redis

with open('../config.py', 'r') as f:
    exec(f.read())

def get_redis(redisinfo):
    redispool = redis.ConnectionPool(host=redisinfo['ip'], port=redisinfo['port'], password=redisinfo['password'],
                                     db=redisinfo['db'])
    return redis.Redis(connection_pool=redispool)


redis0 = get_redis(redisinfo0)
redis3 = get_redis(redisinfo3)
redis4 = get_redis(redisinfo4)
redis15 = get_redis(redisinfo15)


# -------------db0
def insertebn(ebndata):
    return redis0.lpush("HIT_RESULT", ebndata)


# -------------db3
def RDB_set_session_id(session_id):
    return redis3.set('session_id', session_id)


def RDB_get_session_id():
    s = redis3.get('session_id')
    if s:
        return s.decode()
    return ''


def RDB_set_cluster_id(cluster_id):
    return redis3.set('cluster_id', cluster_id)


def RDB_get_cluster_id():
    s = redis3.get('cluster_id')
    if s:
        return s.decode()
    return ''

# -------------db4

def RDB_getfaceacvsall(face_id, hit_face_acvs_dic, acvs_list, start=0, end=-1):
    # 由大到小排列
    arr = redis4.zrevrange(face_id, start, end, True)
    for acvs_score in arr:
        if int(acvs_score[1]) < 300000:
            acvs_list.append(acvs_score[0].decode())
            hit_face_acvs_dic[face_id].append(acvs_score[0].decode())

# -------------db15
def insert(name, key, value):
    # 插入hset
    return redis15.hset(name, key, value)


# 存在1 不存在0 --int
def haskey(name):
    return redis15.exists(name)

def has_h_key(name, key):
    # true or false
    return redis15.hexists(name, key)


def insert_key(name, key, value):
    return redis15.hset(name, key, value)

def RDB_getFace(face_image_id):
    # 获取 id 的信息
    face = redis15.hgetall(face_image_id)  # 获取hashd对应的所有键值
    dface = {}
    for k in face:
        dface[k.decode()] = face[k].decode()
    if dface['is_front'] == 'True':
        dface['is_front'] = True
    if dface['is_front'] == 'False':
        dface['is_front'] = False
    return dface